# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=eventlet
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.25.1
pkgrel=1
pkgdesc="Highly concurrent networking library (mingw-w64)"
arch=('any')
url='https://eventlet.net/'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-greenlet"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python3-monotonic"
             "${MINGW_PACKAGE_PREFIX}-python2-greenlet"
             "${MINGW_PACKAGE_PREFIX}-python2-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python2-monotonic")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-psycopg2"
              "${MINGW_PACKAGE_PREFIX}-python3-nose"
              "${MINGW_PACKAGE_PREFIX}-python3-pyopenssl"
              "${MINGW_PACKAGE_PREFIX}-python3-httplib2"
              "${MINGW_PACKAGE_PREFIX}-python3-mock"
              "${MINGW_PACKAGE_PREFIX}-python3-pyzmq"
              "${MINGW_PACKAGE_PREFIX}-python3-dnspython"
              "${MINGW_PACKAGE_PREFIX}-python2-psycopg2"
              "${MINGW_PACKAGE_PREFIX}-python2-nose"
              "${MINGW_PACKAGE_PREFIX}-python2-pyopenssl"
              "${MINGW_PACKAGE_PREFIX}-python2-httplib2"
              "${MINGW_PACKAGE_PREFIX}-python2-mock"
              "${MINGW_PACKAGE_PREFIX}-python2-pyzmq"
              "${MINGW_PACKAGE_PREFIX}-python2-dnspython"
              "${MINGW_PACKAGE_PREFIX}-python2-subprocess32")
options=('staticlibs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/eventlet/eventlet/archive/v${pkgver}.tar.gz)
sha512sums=('ed3c1bfc17039c17730d4a8bb8ae3a3604b0210f3e6c326764fce748d7466a090b270c5dbc713aaf023681f33a80967adbe83f6280b61f5661a309a0bd43249a')


# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
  sed -r 's|(check_idle_cpu_usage\(.*,) .*\)|\1 0.8\)|g' \
      -i ${_realname}-${pkgver}/tests/*_test.py
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
  sed 's|python|python2|' -i python2-build-${CARCH}/examples/*
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.
build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build 
#    SPHINXBUILD=${MINGW_PREFIX}/bin/sphinx-build.exe make -C doc text
  done  
}

check() {
  msg "Python ${pver} test for ${CARCH}"
  cd "${srcdir}/python3-build-${CARCH}"
  ${MINGW_PREFIX}/bin/nosetests -sv tests || warning "tests failed"

  msg "Python ${pver} test for ${CARCH}"
  cd "${srcdir}/python3-build-${CARCH}"
  ${MINGW_PREFIX}/bin/nosetests2 -sv tests || warning "tests failed"
}

package_python3-eventlet() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-greenlet"
           "${MINGW_PACKAGE_PREFIX}-python3-monotonic")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python3-psycopg2: non-blocking PostgreSQL support"
              "${MINGW_PACKAGE_PREFIX}-python3-pyopenssl: non-blocking SSL support"
              "${MINGW_PACKAGE_PREFIX}-python3-httplib2: non-blocking HTTP support"
              "${MINGW_PACKAGE_PREFIX}-python3-pyzmq: non-blocking ZeroMQ support"
              "${MINGW_PACKAGE_PREFIX}-python3-dnspython: non-blocking DNS support")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
#  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/python3-${_realname}"
#  cp -r _build/text "${pkgdir}${MINGW_PREFIX}/share/doc/python3-${_realname}"
#  cp -r examples "${pkgdir}${MINGW_PREFIX}/share/doc/python3-${_realname}"
}

package_python2-eventlet() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-greenlet"
           "${MINGW_PACKAGE_PREFIX}-python2-monotonic")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-psycopg2: non-blocking PostgreSQL support"
              "${MINGW_PACKAGE_PREFIX}-python2-pyopenssl: non-blocking SSL support"
              "${MINGW_PACKAGE_PREFIX}-python2-httplib2: non-blocking HTTP support"
              "${MINGW_PACKAGE_PREFIX}-python2-pyzmq: non-blocking ZeroMQ support"
              "${MINGW_PACKAGE_PREFIX}-python2-dnspython: non-blocking DNS support")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
#  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/python2-${_realname}"
#  cp -r _build/text "${pkgdir}${MINGW_PREFIX}/share/doc/python2-${_realname}"
#  cp -r examples "${pkgdir}${MINGW_PREFIX}/share/doc/python2-${_realname}"
}

package_mingw-w64-i686-python2-eventlet() {
  package_python2-eventlet
}

package_mingw-w64-i686-python3-eventlet() {
  package_python3-eventlet
}

package_mingw-w64-x86_64-python2-eventlet() {
  package_python2-eventlet
}

package_mingw-w64-x86_64-python3-eventlet() {
  package_python3-eventlet
}
